package com.itheima;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

public class HashSetDemo {

    public static void main(String[] args) {

        HashSet<String> hashSet = new HashSet<>();

        hashSet.add("张三");
        hashSet.add("李四");
        hashSet.add("王五");
        hashSet.add("赵六");
        hashSet.add("李七");
        hashSet.add("吴八");

        //System.out.println(hashSet);

        System.out.println("---------------------迭代器-----------------");
        Iterator<String>iterator = hashSet.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            System.out.println(next);
        }

        // for (int i = 0; i < hashSet.size(); i++) {
        //}hashset不能用for循环

        System.out.println("---------------------增强for-----------------");
        //增强for循环
        for (String s : hashSet) {
            System.out.println(s);
        }
        System.out.println("---------------------匿名内部类-----------------");
        //匿名内部类
        hashSet.forEach(new java.util.function.Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });

        System.out.println("---------------------Lambda表达式-----------------");
        //Lambda表达式遍历
        hashSet.forEach(s -> System.out.println(s));


        System.out.println("---------------------方法引用-----------------");
        hashSet.forEach(System.out::println);

        //ArrayList去重
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("张三");
        arrayList.add("张三");
        arrayList.add("张三");
        arrayList.add("张三");

        // System.out.println(arrayList);

        ArrayList<String> newArrayList = new ArrayList<>(new HashSet<>(arrayList));
        //System.out.println(newArrayList);

    }
}